//
//  ESPhotoTableViewController.m
//  EyeshotPhoto
//
//  Created by admin on 15/7/1.
//  Copyright (c) 2015年 eyeshot. All rights reserved.
//

#import "ESPhotoTableViewController.h"
#import "ESNewPhotoModel.h"
#import "ESPhotoTableViewCell.h"
#import "ESPhotoDetailViewController.h"
#import "ESToolbar.h"
#define kURL @"http://api.budejie.com/api/api_open.php?a=newlist&appname=baisishequ&asid=7AD5815A-BC34-40CA-A9BE-C9DAC444BC95&c=data&client=iphone&device=ios%20%E8%AE%BE%E5%A4%87&from=ios&jbk=0&mac=&market=&openudid=a3f29abd63d37c5476e06c24cad694f1182f25b2&page=0&per=20&sub_flag=1&type=10&udid=&uid=15094607&ver=3.6"

#define kURLLove @"http://api.budejie.com/api/api_open.php?a=love&appname=baisishequ&asid=7AD5815A-BC34-40CA-A9BE-C9DAC444BC95&c=post&client=iphone&device=ios%20%E8%AE%BE%E5%A4%87&id=14728372&jbk=0&mac=&market=&openudid=a3f29abd63d37c5476e06c24cad694f1182f25b2&sex=m&tj_from=image&udid=&uid=15094607&ver=3.6"

@interface ESPhotoTableViewController ()<ESPhotoTableViewCellDelegate,ESToolbarDelegate>

@property (nonatomic, strong) NSMutableArray *modelArr;
@property (nonatomic, strong) ESPhotoDetailViewController *photoDetailVC;
@end

@implementation ESPhotoTableViewController

- (void)requestData
{
    //1.请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:kURL parameters:nil success:^(AFHTTPRequestOperation *operation, NSDictionary *dict) {
        NSArray *arr = dict[@"list"];
        for (NSDictionary *dict1 in arr)
        {
            ESNewPhotoModel *model = [[ESNewPhotoModel alloc] init];
            [model setValuesForKeysWithDictionary:dict1];
            [self.modelArr addObject:model];
        
        }
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"请求失败---%@",error);
    }];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.modelArr = [NSMutableArray arrayWithCapacity:0];
    [self requestData];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
   
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.modelArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"photoCell";
    ESPhotoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (!cell)
    {
       
        cell = [[ESPhotoTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:ID];
    }
    cell.delegate = self;//cell的代理，让图片变大
    cell.bottomView.delegate = self;//成为代理，相应bottom上的按钮事件
    ESNewPhotoModel *model = [self.modelArr objectAtIndex:indexPath.row];
    [cell setPhotoModel:model];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ESNewPhotoModel *model = [self.modelArr objectAtIndex:indexPath.row];
    return [ESPhotoTableViewCell heightForCellWithModel:model];

}

//cell的代理方法，让图片变大
- (void)photoTableViewCell:(ESPhotoTableViewCell *)cell didSeletedImage:(UIImage *)image
{
    self.photoDetailVC = [[ESPhotoDetailViewController alloc] init];
    self.photoDetailVC.imageV.image = image;
    [self presentViewController:self.photoDetailVC animated:YES completion:nil];
}


- (void)ESToolbar:(ESToolbar *)toolbar didSelectBtn:(UIButton *)btn {
    switch (btn.tag) {
        case ESToolbarBtnTypeDing:
        {
            NSURL *url = [NSURL URLWithString:kURLLove];
            NSURLRequest *request = [NSURLRequest requestWithURL:url];
            NSOperationQueue *queue = [NSOperationQueue currentQueue];
            [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                //                NSString *str = [NSString stringWithFormat:@"%@",data];
                //                NSLog(@"***%@",str);
                [btn setImage:[UIImage imageNamed:@"mainCellDingClick"] forState:(UIControlStateNormal)];
                //                [button setTitle:count forState:(UIControlStateNormal)];
                
            }];
        }
            break;
        case ESToolbarBtnTypeCai:
        {
            
        }
            break;
        case ESToolbarBtnTypeShare:
        {
            
        }
            break;
        case ESToolbarBtnTypeComment:
        {
            
        }
            break;
            
        default:
            break;
    }
}

@end
